package uploads_api

import (
	"gin_study_blog/common"
	"gin_study_blog/common/http"
	"gin_study_blog/models"
	"github.com/gin-gonic/gin"
)

type ImageUpdateRequest struct {
	ID   uint   `json:"id" binding:"required" msg:"请选择修改文件"`
	Name string `json:"name" binding:"required" msg:"请输入修改名称"`
}

// ImageUpdateName 修改文件名称
func (UploadsApi) ImageUpdateName(c *gin.Context) {
	var up ImageUpdateRequest
	err := c.ShouldBindJSON(&up)
	if err != nil {
		http.FailWithError(err, &up, c)
		return
	}

	var imageUpdate models.BannerModel
	err = common.DB.Take(&imageUpdate, up.ID).Error
	if err != nil {
		http.FailWithMessage("文件不存在", c)
		return
	}
	err = common.DB.Model(&imageUpdate).Update("name", up.Name).Error
	if err != nil {
		common.Log.Error(err.Error())
		http.FailWithMessage("文件名称修改失败", c)
		return
	}
	http.IsOkWithMessage("文件名称修改成功", c)
	return
}
